package com.im.service.docReview.smo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.im.service.dto.TenderInfo;
import com.im.service.tenderInfo.dao.ITenderInfoOperDAO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.im.MDA;
import com.im.service.docReview.dao.IDocReviewOperDAO;
import com.im.service.docReview.dao.IDocReviewQueryDAO;
import com.im.service.dto.BaseException;
import com.im.service.dto.DocManage;
import com.im.service.dto.DocReviewPlan;
import com.im.service.dto.DocReviewRecord;
import com.im.service.dto.DocReviewResult;
import com.im.service.dto.DocReviewer;
import com.im.service.dto.FileUpload;
import com.im.service.folderManage.dao.IFolderManageOperDAO;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class IDocReviewOperSMOImpl implements IDocReviewOperSMO {
	private static final Logger logger = LogManager.getLogger(IDocReviewOperSMOImpl.class);
	@Autowired
	private IDocReviewOperDAO docReviewOperDAO;
	@Autowired
	private IDocReviewQueryDAO docReviewQueryDAO;
	@Autowired
	private IFolderManageOperDAO folderManageOperDAO;
	@Autowired
	private ITenderInfoOperDAO tenderInfoOperDAO;

	@Override
	public String insertDocReviewInfo(String inJson, String staffId) {
		try {
			String docName = JsonUtil.obtainValueFromJsonObject(inJson, "docName");
			String purchasePlanId = JsonUtil.obtainValueFromJsonObject(inJson, "purchasePlanId");
			String docContent = JsonUtil.obtainValueFromJsonObject(inJson, "docContent");
			String docCode = JsonUtil.obtainValueFromJsonObject(inJson, "docCode");
			String fileAttr = JsonUtil.obtainValueFromJsonObject(inJson, "fileAttr");
			String fileType = JsonUtil.obtainValueFromJsonObject(inJson, "fileType");
			String filePath = "标书未上传";
			String docManageDescription = JsonUtil.obtainValueFromJsonObject(inJson, "docManageDescription");

//			Map<String, Object> docManageMap = new HashMap<String, Object>();
//			docManageMap.put("purchasePlanId", purchasePlanId);
//			DocManage dManage = docReviewQueryDAO.queryDocManageByPurchPlanId(docManageMap);
			// 保存文件信息
			String tendInfoId =UUID.randomUUID().toString();
			DocManage docManage = new DocManage();
			String docManageId = UUID.randomUUID().toString();
			docManage.setDocManageId(docManageId);
			docManage.setDocName(docName);
			docManage.setDocType("10");// 文件类型，标书-10
			docManage.setRelaObjType("30");// 文件来源类型，采购计划-10
			docManage.setRelaObjId(tendInfoId);
			docManage.setDocContent(docContent);
			docManage.setDocCode(docCode);
			docManage.setFilePath(filePath);
			docManage.setStatusCd(10);// 状态，有效-10
			docManage.setDescription(docManageDescription);
			docManage.setCreater(staffId);
			docReviewOperDAO.insertDocManageInfo(docManage);

			// 内部评审计划
			Boolean isEmpty = true;
			String internalReviewPlanId = UUID.randomUUID().toString();
			String internalReviewLeadingPerson = JsonUtil.obtainValueFromJsonObject(inJson,
					"internalReviewLeadingPerson");
			String internalReviewLinkman = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewLinkman");
			String internalReviewPlanReviewDt = JsonUtil.obtainValueFromJsonObject(inJson,
					"internalReviewPlanReviewDt");
			String internalReviewAddr = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewAddr");
			String internalReviewDesc = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewDesc");
			String internalReviewResult = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewResult");
			if (internalReviewLeadingPerson!=null && ! "".equals(internalReviewLeadingPerson)) {
				isEmpty = false;
			}
			if (internalReviewLinkman!=null && ! "".equals(internalReviewLinkman)) {
				isEmpty = false;
			}
			if (internalReviewAddr!=null && ! "".equals(internalReviewAddr)) {
				isEmpty = false;
			}
			if (internalReviewDesc!=null && ! "".equals(internalReviewDesc)) {
				isEmpty = false;
			}
			if (!isEmpty) {
				DocReviewPlan internalReviewPlan = new DocReviewPlan();
				internalReviewPlan.setReviewPlanId(internalReviewPlanId);
				internalReviewPlan.setDocManageId(docManageId);
				internalReviewPlan.setLeadingPerson(internalReviewLeadingPerson);
				internalReviewPlan.setLinkman(internalReviewLinkman);
				internalReviewPlan.setStatusCd(40);
				internalReviewPlan.setPlanReviewDt(internalReviewPlanReviewDt);
				internalReviewPlan.setRevierType("10");// 评审类型：内部评审-10
				internalReviewPlan.setAddr(internalReviewAddr);
				internalReviewPlan.setDescription(internalReviewResult);
				docReviewOperDAO.insertDocReviewPlanInfo(internalReviewPlan);
				// 内部评审记录
				String internalReviewRecordId = UUID.randomUUID().toString();
				String internalReviewPartyList = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewParty");
				DocReviewRecord internalReviewRecord = new DocReviewRecord();
				internalReviewRecord.setReviewRecordId(internalReviewRecordId);
				internalReviewRecord.setReviewPlanId(internalReviewPlanId);
				internalReviewRecord.setLeadingPerson(internalReviewLeadingPerson);
				internalReviewRecord.setLinkman(internalReviewLinkman);
				internalReviewRecord.setRevierDt(internalReviewPlanReviewDt);
				internalReviewRecord.setAddr(internalReviewAddr);
				internalReviewRecord.setRevierType("10");// 评审类型，内部评审 - 10
				internalReviewRecord.setParty(internalReviewPartyList);
				internalReviewRecord.setReviewResult(internalReviewResult);
				internalReviewRecord.setCreater(staffId);
				docReviewOperDAO.insertDocReviewRecordInfo(internalReviewRecord);
				// 参与人落表
				List<DocReviewer> internalReviewers = new ArrayList<DocReviewer>();
				String[] internalReviewPartys = internalReviewPartyList.split(",");
				for (String internalReviewParty : internalReviewPartys) {
					String docReviewerId = UUID.randomUUID().toString();
					DocReviewer docReviewer = new DocReviewer();
					docReviewer.setDocReviewerId(docReviewerId);
					docReviewer.setReviewPlanId(internalReviewPlanId);
					docReviewer.setReviewer(internalReviewParty);
					internalReviewers.add(docReviewer);
				}
				docReviewOperDAO.insertDocReviewers(internalReviewers);
			}

			// 专家评审计划
			String expertReviewPlanId = UUID.randomUUID().toString();
			String expertReviewLeadingPerson = JsonUtil.obtainValueFromJsonObject(inJson,
					"expertReviewLeadingPerson");
			String expertReviewLinkman = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewLinkman");
			String expertReviewPlanReviewDt = JsonUtil.obtainValueFromJsonObject(inJson,
					"expertReviewPlanReviewDt");
			String expertReviewAddr = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewAddr");
			String expertReviewDesc = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewDesc");
			String expertReviewResult = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewResult");

			isEmpty = true;
			if (expertReviewLeadingPerson!=null && ! "".equals(expertReviewLeadingPerson)) {
				isEmpty = false;
			}
			if (expertReviewLinkman!=null && ! "".equals(expertReviewLinkman)) {
				isEmpty = false;
			}
			if (expertReviewAddr!=null && ! "".equals(expertReviewAddr)) {
				isEmpty = false;
			}
			if (expertReviewDesc!=null && ! "".equals(expertReviewDesc)) {
				isEmpty = false;
			}
			if (!isEmpty) {
				DocReviewPlan expertReviewPlan = new DocReviewPlan();
				expertReviewPlan.setReviewPlanId(expertReviewPlanId);
				expertReviewPlan.setDocManageId(docManageId);
				expertReviewPlan.setLeadingPerson(expertReviewLeadingPerson);
				expertReviewPlan.setLinkman(expertReviewLinkman);
				expertReviewPlan.setStatusCd(40);
				expertReviewPlan.setPlanReviewDt(expertReviewPlanReviewDt);
				expertReviewPlan.setRevierType("20");// 评审类型：专家评审-20
				expertReviewPlan.setAddr(expertReviewAddr);
				expertReviewPlan.setDescription(expertReviewResult);
				docReviewOperDAO.insertDocReviewPlanInfo(expertReviewPlan);
				// 专家评审记录
				String expertReviewRecordId = UUID.randomUUID().toString();
				String expertReviewPartyList = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewParty");

				DocReviewRecord expertReviewRecord = new DocReviewRecord();
				expertReviewRecord.setReviewRecordId(expertReviewRecordId);
				expertReviewRecord.setReviewPlanId(expertReviewPlanId);
				expertReviewRecord.setLeadingPerson(expertReviewLeadingPerson);
				expertReviewRecord.setLinkman(expertReviewLinkman);
				expertReviewRecord.setRevierDt(expertReviewPlanReviewDt);
				expertReviewRecord.setAddr(expertReviewAddr);
				expertReviewRecord.setRevierType("20");// 评审类型，专家评审 - 20
				expertReviewRecord.setParty(expertReviewPartyList);
				expertReviewRecord.setReviewResult(expertReviewResult);
				expertReviewRecord.setCreater(staffId);
				docReviewOperDAO.insertDocReviewRecordInfo(expertReviewRecord);
				// 参与人落表
				List<DocReviewer> expertReviewers = new ArrayList<DocReviewer>();
				String[] expertReviewPartys = expertReviewPartyList.split(",");
				for (String expertReviewParty : expertReviewPartys) {
					String docReviewerId = UUID.randomUUID().toString();
					DocReviewer docReviewer = new DocReviewer();
					docReviewer.setDocReviewerId(docReviewerId);
					docReviewer.setReviewPlanId(expertReviewPlanId);
					docReviewer.setReviewer(expertReviewParty);
					expertReviewers.add(docReviewer);
				}
				docReviewOperDAO.insertDocReviewers(expertReviewers);
			}

			// 文件评审结果
			String reviewResultId = UUID.randomUUID().toString();
			String reviewResultLeadingPerson = JsonUtil.obtainValueFromJsonObject(inJson,
					"reviewResultLeadingPerson");
			String reviewResultLinkman = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResultLinkman");
			String finalDt = JsonUtil.obtainValueFromJsonObject(inJson, "finalDt");
			String reviewResult = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResult");
			String reviewResultDesc = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResultDesc");
			String reviewResultStatus = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResultStatus");
			isEmpty = true;
			if (reviewResultLeadingPerson!=null && ! "".equals(reviewResultLeadingPerson)) {
				isEmpty = false;
			}
			if (reviewResultLinkman!=null && ! "".equals(reviewResultLinkman)) {
				isEmpty = false;
			}
			if (finalDt!=null && ! "".equals(finalDt)) {
				isEmpty = false;
			}
			if (reviewResultDesc!=null && ! "".equals(reviewResultDesc)) {
				isEmpty = false;
			}
			if (!isEmpty) {
				DocReviewResult docReviewResult = new DocReviewResult();
				docReviewResult.setReviewResultId(reviewResultId);
				docReviewResult.setDocManageId(docManageId);
				docReviewResult.setLeadingPerson(reviewResultLeadingPerson);
				docReviewResult.setLinkman(reviewResultLinkman);
				docReviewResult.setStatusCd(40);
				docReviewResult.setFinalDt(finalDt);
				docReviewResult.setReviewResult(reviewResult);
				docReviewResult.setCreater(staffId);
				docReviewResult.setDescription(reviewResultDesc);
				docReviewOperDAO.insertDocReviewResultInfo(docReviewResult);
			}


			//保存附件信息
			JSONObject param = JSONObject.fromObject(inJson);
			if (param.containsKey("fileList")) {
				JSONArray fileListArray = param.getJSONArray("fileList");
				if (fileListArray != null) {
					List<FileUpload> fileUploads = new ArrayList<FileUpload>();
					for (Object fileUpload : fileListArray) {
						JSONObject fileUploadJson = JSONObject.fromObject(fileUpload);
						FileUpload fileUploadInfo = new FileUpload();
						String uploadId = UUID.randomUUID().toString();
						String uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileUploadJson.getString("attachSuffix").toLowerCase());
						fileUploadInfo.setUploadId(uploadId);
						fileUploadInfo.setUploadName(fileUploadJson.getString("attachFullName"));
						fileUploadInfo.setUploadObjType("60");
						fileUploadInfo.setUploadObjId(docManageId);
						fileUploadInfo.setUploadType(uploadType);
						fileUploadInfo.setFileSuffix(fileUploadJson.getString("attachSuffix"));
						fileUploadInfo.setFileAttr(fileAttr);
						fileUploadInfo.setFileType(fileType);
						fileUploadInfo.setUploadSize(fileUploadJson.getLong("attachSize"));
						fileUploadInfo.setFilePath(fileUploadJson.getString("attachPath"));
						fileUploadInfo.setUploadDate(fileUploadJson.getString("uploadDt"));
						fileUploads.add(fileUploadInfo);
					}
					folderManageOperDAO.insertFileUploadInfos(fileUploads);
				}
			}

			//保存招标信息
			String implProjId = JsonUtil.obtainValueFromJsonObject(inJson, "impleProjId");
			String tenderInfoName = docCode+docName;
			// 招标书信息
			TenderInfo tenderInfo = new TenderInfo();
			tenderInfo.setDocManageId("".equals(docManageId)?null:docManageId);
			tenderInfo.setImpleProjId(implProjId);
			if (!purchasePlanId.equals("-1")) {
				tenderInfo.setPurchPlanId(purchasePlanId);
			}
			tenderInfo.setTenderInfoId(tendInfoId);
			tenderInfo.setTenderCode(tendInfoId);
			tenderInfo.setCreateDt(new Date());
			tenderInfo.setStatusCd(10);
			tenderInfo.setTenderTitle(tenderInfoName);
			tenderInfoOperDAO.saveTenderInfo(tenderInfo);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.toString());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.toString());
		}
	}

	@Override
	public String updateDocReviewInfo(String inJson, String staffId) {
		try {
			Boolean isEmpty = true;
			String docName = JsonUtil.obtainValueFromJsonObject(inJson, "docName");
			String docManageId = JsonUtil.obtainValueFromJsonObject(inJson, "docManageId");
			String docContent = JsonUtil.obtainValueFromJsonObject(inJson, "docContent");
			String docCode = JsonUtil.obtainValueFromJsonObject(inJson, "docCode");
			String fileAttr = JsonUtil.obtainValueFromJsonObject(inJson, "fileAttr");
			String fileType = JsonUtil.obtainValueFromJsonObject(inJson, "fileType");
			// String filePath = JsonUtil.obtainValueFromJsonObject(inJson,
			// "filePath");
			String docManageDescription = JsonUtil.obtainValueFromJsonObject(inJson, "docManageDescription");
			// 修改文件信息
			DocManage docManage = new DocManage();
			docManage.setDocManageId(docManageId);
			docManage.setDocName(docName);
			docManage.setDocContent(docContent);
			docManage.setDocCode(docCode);
			// docManage.setFilePath(filePath);
			docManage.setDescription(docManageDescription);
			docManage.setModifier(staffId);
			docReviewOperDAO.updateDocManageInfo(docManage);

			// 内部评审计划
			String internalReviewPlanId = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewPlanId");
			String internalReviewLeadingPerson = JsonUtil.obtainValueFromJsonObject(inJson,
					"internalReviewLeadingPerson");
			String internalReviewLinkman = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewLinkman");
			String internalReviewPlanReviewDt = JsonUtil.obtainValueFromJsonObject(inJson,
					"internalReviewPlanReviewDt");
			String internalReviewAddr = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewAddr");
			String internalReviewDesc = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewDesc");
			String internalReviewResult = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewResult");

			//String internalReviewPlanId = UUID.randomUUID().toString();
			//原来就是空的
			if (internalReviewLeadingPerson != null && !"".equals(internalReviewLeadingPerson)) {
				isEmpty = false;
			}
			if (internalReviewLinkman != null && !"".equals(internalReviewLinkman)) {
				isEmpty = false;
			}
			if (internalReviewPlanReviewDt != null && !"".equals(internalReviewPlanReviewDt)) {
				isEmpty = false;
			}
			if (internalReviewAddr != null && !"".equals(internalReviewAddr)) {
				isEmpty = false;
			}
			if (internalReviewDesc != null && !"".equals(internalReviewDesc)) {
				isEmpty = false;
			}
			if (!isEmpty){
				if (internalReviewPlanId == null || "".equals(internalReviewPlanId)) {
					internalReviewPlanId = UUID.randomUUID().toString();
					DocReviewPlan internalReviewPlan = new DocReviewPlan();
					internalReviewPlan.setReviewPlanId(internalReviewPlanId);
					internalReviewPlan.setDocManageId(docManageId);
					internalReviewPlan.setLeadingPerson(internalReviewLeadingPerson);
					internalReviewPlan.setLinkman(internalReviewLinkman);
					internalReviewPlan.setStatusCd(40);
					internalReviewPlan.setPlanReviewDt(internalReviewPlanReviewDt);
					internalReviewPlan.setRevierType("10");// 评审类型：内部评审-10
					internalReviewPlan.setAddr(internalReviewAddr);
					internalReviewPlan.setDescription(internalReviewResult);
					docReviewOperDAO.insertDocReviewPlanInfo(internalReviewPlan);
					// 内部评审记录
					String internalReviewRecordId = UUID.randomUUID().toString();
					String internalReviewPartyList = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewParty");
					DocReviewRecord internalReviewRecord = new DocReviewRecord();
					internalReviewRecord.setReviewRecordId(internalReviewRecordId);
					internalReviewRecord.setReviewPlanId(internalReviewPlanId);
					internalReviewRecord.setLeadingPerson(internalReviewLeadingPerson);
					internalReviewRecord.setLinkman(internalReviewLinkman);
					internalReviewRecord.setRevierDt(internalReviewPlanReviewDt);
					internalReviewRecord.setAddr(internalReviewAddr);
					internalReviewRecord.setRevierType("10");// 评审类型，内部评审 - 10
					internalReviewRecord.setParty(internalReviewPartyList);
					internalReviewRecord.setReviewResult(internalReviewResult);
					internalReviewRecord.setCreater(staffId);
					docReviewOperDAO.insertDocReviewRecordInfo(internalReviewRecord);
					// 参与人落表
					List<DocReviewer> internalReviewers = new ArrayList<DocReviewer>();
					String[] internalReviewPartys = internalReviewPartyList.split(",");
					for (String internalReviewParty : internalReviewPartys) {
						String docReviewerId = UUID.randomUUID().toString();
						DocReviewer docReviewer = new DocReviewer();
						docReviewer.setDocReviewerId(docReviewerId);
						docReviewer.setReviewPlanId(internalReviewPlanId);
						docReviewer.setReviewer(internalReviewParty);
						internalReviewers.add(docReviewer);
					}
					docReviewOperDAO.insertDocReviewers(internalReviewers);
				} else {
					DocReviewPlan internalReviewPlan = new DocReviewPlan();
					internalReviewPlan.setReviewPlanId(internalReviewPlanId);
					internalReviewPlan.setDocManageId(docManageId);
					internalReviewPlan.setLeadingPerson(internalReviewLeadingPerson);
					internalReviewPlan.setLinkman(internalReviewLinkman);
					internalReviewPlan.setStatusCd(40);//默认 40 评审通过
					internalReviewPlan.setPlanReviewDt(internalReviewPlanReviewDt);
					internalReviewPlan.setAddr(internalReviewAddr);
					internalReviewPlan.setDescription(internalReviewDesc);
					docReviewOperDAO.updateDocReviewPlanInfo(internalReviewPlan);
					// 内部评审记录
					String internalReviewRecordId = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewRecordId");
					String internalReviewPartyList = JsonUtil.obtainValueFromJsonObject(inJson, "internalReviewParty");
					DocReviewRecord internalReviewRecord = new DocReviewRecord();
					internalReviewRecord.setReviewRecordId(internalReviewRecordId);
					internalReviewRecord.setReviewPlanId(internalReviewPlanId);
					internalReviewRecord.setLeadingPerson(internalReviewLeadingPerson);
					internalReviewRecord.setLinkman(internalReviewLinkman);
					internalReviewRecord.setRevierDt(internalReviewPlanReviewDt);
					internalReviewRecord.setAddr(internalReviewAddr);
					internalReviewRecord.setParty(internalReviewPartyList);
					internalReviewRecord.setReviewResult(internalReviewResult);
					internalReviewRecord.setModifier(staffId);
					docReviewOperDAO.updateDocReviewRecordInfo(internalReviewRecord);
					// 参与人落表
					Map<String, Object> internalReviewRecordMap = new HashMap<String, Object>();
					internalReviewRecordMap.put("reviewPlanId", internalReviewPlanId);
					List<DocReviewer> internalReviewerList = docReviewQueryDAO
							.queryDocReviewerByReviewPlanId(internalReviewRecordMap);
					if (internalReviewerList.size() > 0) {
						docReviewOperDAO.deleteDocReviewer(internalReviewRecordMap);
					}
					List<DocReviewer> internalReviewers = new ArrayList<DocReviewer>();
					String[] internalReviewPartys = internalReviewPartyList.split(",");
					for (String internalReviewParty : internalReviewPartys) {
						String docReviewerId = UUID.randomUUID().toString();
						DocReviewer docReviewer = new DocReviewer();
						docReviewer.setDocReviewerId(docReviewerId);
						docReviewer.setReviewPlanId(internalReviewPlanId);
						docReviewer.setReviewer(internalReviewParty);
						internalReviewers.add(docReviewer);
					}
					docReviewOperDAO.insertDocReviewers(internalReviewers);
				}
			}
			// 专家评审计划
			String expertReviewPlanId = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewPlanId");
			String expertReviewLeadingPerson = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewLeadingPerson");
			String expertReviewLinkman = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewLinkman");
			String expertReviewPlanReviewDt = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewPlanReviewDt");
			String expertReviewAddr = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewAddr");
			String expertReviewDesc = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewDesc");
			String expertReviewResult = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewResult");
			isEmpty = true;
			//原来就是空的
			if (expertReviewLeadingPerson!=null && ! "".equals(expertReviewLeadingPerson)) {
				isEmpty = false;
			}
			if (expertReviewLinkman!=null && ! "".equals(expertReviewLinkman)) {
				isEmpty = false;
			}
			if (expertReviewPlanReviewDt!=null && ! "".equals(expertReviewPlanReviewDt)) {
				isEmpty = false;
			}
			if (expertReviewAddr!=null && ! "".equals(expertReviewAddr)) {
				isEmpty = false;
			}
			if (expertReviewDesc!=null && ! "".equals(expertReviewDesc)) {
				isEmpty = false;
			}
			if (!isEmpty) {
				if (expertReviewPlanId == null ||"".equals(expertReviewPlanId)) {
					expertReviewPlanId = UUID.randomUUID().toString();
					DocReviewPlan expertReviewPlan = new DocReviewPlan();
					expertReviewPlan.setReviewPlanId(expertReviewPlanId);
					expertReviewPlan.setDocManageId(docManageId);
					expertReviewPlan.setLeadingPerson(expertReviewLeadingPerson);
					expertReviewPlan.setLinkman(expertReviewLinkman);
					expertReviewPlan.setStatusCd(40);
					expertReviewPlan.setPlanReviewDt(expertReviewPlanReviewDt);
					expertReviewPlan.setRevierType("20");// 评审类型：专家评审-20
					expertReviewPlan.setAddr(expertReviewAddr);
					expertReviewPlan.setDescription(expertReviewResult);
					docReviewOperDAO.insertDocReviewPlanInfo(expertReviewPlan);
					// 专家评审记录
					String expertReviewRecordId = UUID.randomUUID().toString();
					String expertReviewPartyList = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewParty");

					DocReviewRecord expertReviewRecord = new DocReviewRecord();
					expertReviewRecord.setReviewRecordId(expertReviewRecordId);
					expertReviewRecord.setReviewPlanId(expertReviewPlanId);
					expertReviewRecord.setLeadingPerson(expertReviewLeadingPerson);
					expertReviewRecord.setLinkman(expertReviewLinkman);
					expertReviewRecord.setRevierDt(expertReviewPlanReviewDt);
					expertReviewRecord.setAddr(expertReviewAddr);
					expertReviewRecord.setRevierType("20");// 评审类型，专家评审 - 20
					expertReviewRecord.setParty(expertReviewPartyList);
					expertReviewRecord.setReviewResult(expertReviewResult);
					expertReviewRecord.setCreater(staffId);
					docReviewOperDAO.insertDocReviewRecordInfo(expertReviewRecord);
					// 参与人落表
					List<DocReviewer> expertReviewers = new ArrayList<DocReviewer>();
					String[] expertReviewPartys = expertReviewPartyList.split(",");
					for (String expertReviewParty : expertReviewPartys) {
						String docReviewerId = UUID.randomUUID().toString();
						DocReviewer docReviewer = new DocReviewer();
						docReviewer.setDocReviewerId(docReviewerId);
						docReviewer.setReviewPlanId(expertReviewPlanId);
						docReviewer.setReviewer(expertReviewParty);
						expertReviewers.add(docReviewer);
					}
					docReviewOperDAO.insertDocReviewers(expertReviewers);
				} else {
					DocReviewPlan expertReviewPlan = new DocReviewPlan();
					expertReviewPlan.setReviewPlanId(expertReviewPlanId);
					expertReviewPlan.setDocManageId(docManageId);
					expertReviewPlan.setLeadingPerson(expertReviewLeadingPerson);
					expertReviewPlan.setLinkman(expertReviewLinkman);
					expertReviewPlan.setStatusCd(40);//默认 40 评审通过
					expertReviewPlan.setPlanReviewDt(expertReviewPlanReviewDt);
					expertReviewPlan.setAddr(expertReviewAddr);
					expertReviewPlan.setDescription(expertReviewDesc);
					docReviewOperDAO.updateDocReviewPlanInfo(expertReviewPlan);
					// 专家评审记录
					String expertReviewRecordId = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewRecordId");
					String expertReviewPartyList = JsonUtil.obtainValueFromJsonObject(inJson, "expertReviewParty");
					DocReviewRecord expertReviewRecord = new DocReviewRecord();
					expertReviewRecord.setReviewRecordId(expertReviewRecordId);
					expertReviewRecord.setReviewPlanId(expertReviewPlanId);
					expertReviewRecord.setLeadingPerson(expertReviewLeadingPerson);
					expertReviewRecord.setLinkman(expertReviewLinkman);
					expertReviewRecord.setRevierDt(expertReviewPlanReviewDt);
					expertReviewRecord.setAddr(expertReviewAddr);
					expertReviewRecord.setParty(expertReviewPartyList);
					expertReviewRecord.setReviewResult(expertReviewResult);
					expertReviewRecord.setModifier(staffId);
					docReviewOperDAO.updateDocReviewRecordInfo(expertReviewRecord);
					// 参与人落表
					Map<String, Object> expertReviewRecordMap = new HashMap<String, Object>();
					expertReviewRecordMap.put("reviewPlanId", expertReviewPlanId);
					List<DocReviewer> expertReviewerList = docReviewQueryDAO
							.queryDocReviewerByReviewPlanId(expertReviewRecordMap);
					if (expertReviewerList.size() > 0) {
						docReviewOperDAO.deleteDocReviewer(expertReviewRecordMap);
					}
					List<DocReviewer> expertReviewers = new ArrayList<DocReviewer>();
					String[] expertReviewPartys = expertReviewPartyList.split(",");
					for (String expertReviewParty : expertReviewPartys) {
						String docReviewerId = UUID.randomUUID().toString();
						DocReviewer docReviewer = new DocReviewer();
						docReviewer.setDocReviewerId(docReviewerId);
						docReviewer.setReviewPlanId(expertReviewPlanId);
						docReviewer.setReviewer(expertReviewParty);
						expertReviewers.add(docReviewer);
					}
					docReviewOperDAO.insertDocReviewers(expertReviewers);
				}
			}
			// 文件评审结果
			String reviewResultId = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResultId");
			String reviewResultLeadingPerson = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResultLeadingPerson");
			String reviewResultLinkman = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResultLinkman");
			String finalDt = JsonUtil.obtainValueFromJsonObject(inJson, "finalDt");
			String reviewResult = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResult");
			String reviewResultDesc = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResultDesc");
			String reviewResultStatus = JsonUtil.obtainValueFromJsonObject(inJson, "reviewResultStatus");
			DocReviewResult docReviewResult = new DocReviewResult();
			docReviewResult.setReviewResultId(reviewResultId);
			docReviewResult.setDocManageId(docManageId);
			docReviewResult.setLeadingPerson(reviewResultLeadingPerson);
			docReviewResult.setLinkman(reviewResultLinkman);
			docReviewResult.setStatusCd(40);
			docReviewResult.setFinalDt(finalDt);
			docReviewResult.setReviewResult(reviewResult);
			docReviewResult.setModifier(staffId);
			docReviewResult.setDescription(reviewResultDesc);
			docReviewOperDAO.updateDocReviewResultInfo(docReviewResult);
			
			//保存附件信息
			JSONObject param = JSONObject.fromObject(inJson);
			if (param.containsKey("fileList")) {
				JSONArray fileListArray = param.getJSONArray("fileList");
				if (fileListArray != null) {
					List<FileUpload> fileUploads = new ArrayList<FileUpload>();
					for (Object fileUpload : fileListArray) {
						JSONObject fileUploadJson = JSONObject.fromObject(fileUpload);
						FileUpload fileUploadInfo = new FileUpload();
						String uploadId = UUID.randomUUID().toString();
						String uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileUploadJson.getString("attachSuffix").toLowerCase());
						fileUploadInfo.setUploadId(uploadId);
						fileUploadInfo.setUploadName(fileUploadJson.getString("attachFullName"));
						fileUploadInfo.setUploadObjType("60");
						fileUploadInfo.setUploadObjId(docManageId);
						fileUploadInfo.setUploadType(uploadType);
						fileUploadInfo.setFileSuffix(fileUploadJson.getString("attachSuffix"));
						fileUploadInfo.setFileAttr(fileAttr);
						fileUploadInfo.setFileType(fileType);
						fileUploadInfo.setUploadSize(fileUploadJson.getLong("attachSize"));
						fileUploadInfo.setFilePath(fileUploadJson.getString("attachPath"));
						fileUploadInfo.setUploadDate(fileUploadJson.getString("uploadDt"));
						fileUploads.add(fileUploadInfo);
					}
					folderManageOperDAO.insertFileUploadInfos(fileUploads);
				}
			}

			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.toString());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.toString());
		}
	}

	@Override
	public String insertFileUploadInfoForDocReview(String inJson, String staffId) {
		try {
//			String uploadType = "";
			String docManageId = JsonUtil.obtainValueFromJsonObject(inJson, "docManageId");
//			String uploadName = JsonUtil.obtainValueFromJsonObject(inJson, "uploadName");
//			String fileSuffix = JsonUtil.obtainValueFromJsonObject(inJson, "fileSuffix");
			String fileAttr = JsonUtil.obtainValueFromJsonObject(inJson, "fileAttr");
			String fileType = JsonUtil.obtainValueFromJsonObject(inJson, "fileType");
//			String uploadSize = JsonUtil.obtainValueFromJsonObject(inJson, "uploadSize");
//			String filePath = JsonUtil.obtainValueFromJsonObject(inJson, "filePath");
//			String uploadDate = JsonUtil.obtainValueFromJsonObject(inJson, "uploadDt");
//			String uploadDescription = JsonUtil.obtainValueFromJsonObject(inJson, "uploadDescription");
//			Map<String, Object> fileUploadMap = new HashMap<String, Object>();
//			fileUploadMap.put("uploadObjId", docManageId);
//			fileUploadMap.put("uploadObjType", "60");
//			FileUpload fileUpload = docReviewQueryDAO.queryFileUploadInfoDetailForDocManage(fileUploadMap);
//			if (fileUpload != null) {
//				FileUpload fileUploadInfo = new FileUpload();
//				fileUploadInfo.setUploadId(fileUpload.getUploadId());
//				fileUploadInfo.setUploadName(uploadName);
//				if (!"".equals(fileSuffix)) {
//					uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileSuffix.toLowerCase());
//				}
//				fileUploadInfo.setUploadType(uploadType);
//				fileUploadInfo.setFileSuffix(fileSuffix);
//				fileUploadInfo.setFileAttr(fileAttr);
//				fileUploadInfo.setFileType(fileType);
//				fileUploadInfo.setUploadSize(Long.valueOf(uploadSize));
//				fileUploadInfo.setFilePath(filePath);
//				fileUploadInfo.setUploadDate(uploadDate);
//				fileUploadInfo.setDescription(uploadDescription);
//				folderManageOperDAO.updateFileUploadInfo(fileUploadInfo);
//			} else {
//				FileUpload fileUploadInfo = new FileUpload();
//				if (!"".equals(fileSuffix)) {
//					uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileSuffix.toLowerCase());
//				}
//				String uploadId = UUID.randomUUID().toString();
//				fileUploadInfo.setUploadId(uploadId);
//				fileUploadInfo.setUploadName(uploadName);
//				fileUploadInfo.setUploadObjType("60");
//				fileUploadInfo.setUploadObjId(docManageId);
//				fileUploadInfo.setUploadType(uploadType);
//				fileUploadInfo.setFileSuffix(fileSuffix);
//				fileUploadInfo.setFileAttr(fileAttr);
//				fileUploadInfo.setFileType(fileType);
//				fileUploadInfo.setUploadSize(Long.valueOf(uploadSize));
//				fileUploadInfo.setFilePath(filePath);
//				fileUploadInfo.setUploadDate(uploadDate);
//				fileUploadInfo.setDescription(uploadDescription);
//				folderManageOperDAO.insertFileUploadInfo(fileUploadInfo);
//			}
//			if (!"".equals(filePath)) {
//				DocManage docM = new DocManage();
//				docM.setDocManageId(docManageId);
//				docM.setFilePath(filePath);
//				docReviewOperDAO.updateFilePathForDocManage(docM);
//			}
			//保存附件信息
			JSONObject param = JSONObject.fromObject(inJson);
			if (param.containsKey("fileList")) {
				JSONArray fileListArray = param.getJSONArray("fileList");
				if (fileListArray != null) {
					List<FileUpload> fileUploads = new ArrayList<FileUpload>();
					for (Object fileUpload : fileListArray) {
						JSONObject fileUploadJson = JSONObject.fromObject(fileUpload);
						FileUpload fileUploadInfo = new FileUpload();
						String uploadId = UUID.randomUUID().toString();
						String uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileUploadJson.getString("attachSuffix").toLowerCase());
						fileUploadInfo.setUploadId(uploadId);
						fileUploadInfo.setUploadName(fileUploadJson.getString("attachFullName"));
						fileUploadInfo.setUploadObjType("60");
						fileUploadInfo.setUploadObjId(docManageId);
						fileUploadInfo.setUploadType(uploadType);
						fileUploadInfo.setFileSuffix(fileUploadJson.getString("attachSuffix"));
						fileUploadInfo.setFileAttr(fileAttr);
						fileUploadInfo.setFileType(fileType);
						fileUploadInfo.setUploadSize(fileUploadJson.getLong("attachSize"));
						fileUploadInfo.setFilePath(fileUploadJson.getString("attachPath"));
						fileUploadInfo.setUploadDate(fileUploadJson.getString("uploadDt"));
						fileUploads.add(fileUploadInfo);
					}
					folderManageOperDAO.insertFileUploadInfos(fileUploads);
				}
			}
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.toString());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.toString());
		}
	}

	@Override
	public String updateBatchDocManage(String inJson, String staffId) {
		try {
			String docManageIdList = JsonUtil.obtainValueFromJsonObject(inJson, "docManageIds");
			String[] docManageIds = docManageIdList.split(",");
			Map<String, Object> docManageMap = new HashMap<String, Object>();
			docManageMap.put("docManageIds", docManageIds);
			docManageMap.put("statusCd", 20);// 失效：20
			docManageMap.put("modifier", staffId);
			docReviewOperDAO.updateBatchDocManage(docManageMap);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.toString());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.toString());
		}
	}
}
